package sapling.api;

import io.swagger.annotations.ApiOperation;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sapling.api.model.ApiLandList;
import sapling.api.model.ApiLandMassModel;
import sapling.api.model.WebLandlist;
import sapling.dao.AreaLocationDao;
import sapling.dao.LandMassDao;
import sapling.dao.TreeDao;
import sapling.dao.TreeWellDao;
import sapling.model.AreaLocation;
import sapling.model.Drawland;
import sapling.model.LandMass;
import sapling.model.Tree;
import sapling.service.LandMassService;
import sapling.service.StorageService;
import sapling.utils.Msg;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by mac on 17/7/11.
 */
@RestController
@RequestMapping("/api/area")
public class AreaApi {
    @Autowired
    private AreaLocationDao areaLocationDao;
    @Autowired
    private LandMassDao landMassDao;
    @Autowired
    private LandMassService landMassService;
    @Autowired
    private TreeWellDao treeWellDao;
    @Autowired
    StorageService storageService;
    @Autowired
    TreeDao treeDao;

    @GetMapping("/deltreewell")
    public Msg<String> deltreewell(@RequestParam long landmassid){
        LandMass landMass = landMassDao.findOne(landmassid);
        List<Tree> trees = treeDao.findBylandmass(landmassid);
        StringBuilder treeidsb=new StringBuilder();
        if(trees!=null&&trees.size()>0){
            for(Tree tree:trees){
                if(tree.getTree_well_id()!=0) {
                    treeidsb.append(";");
                    treeidsb.append(tree.getTree_id());
                }
            }
            int out = storageService.out(treeidsb.toString(), "root");
        }
        areaLocationDao.deltreewell(landmassid);
        landMass.setGenerate(0);
        landMassDao.saveAndFlush(landMass);
        return Msg.newMsg("success");
    }
    @GetMapping("/listArea")
    public Msg<List<AreaLocation>> listArea() {
        List<AreaLocation> all = areaLocationDao.findAll();
        for (AreaLocation single : all) {
            single.setRadius(landMassDao.getAreacount(single.getAreaLocation_id()));
            single.setRoadBreadth(areaLocationDao.getTreecount(single.getAreaLocation_id()));
            List<String> alltreename = areaLocationDao.getAlltreename(single.getAreaLocation_id());
            StringBuilder spec = new StringBuilder();
            for (String t : alltreename) {
                spec.append(t + ";");
            }
            if (alltreename.size() > 0) {
                single.setTreespec(spec.toString().substring(0, spec.toString().length() - 1));
            } else {
                single.setTreespec("");
            }
            for(LandMass landMass:single.getLandMasses()) {
                landMass.setPlant(treeWellDao.getCountisUse(1, landMass.getLandMass_id()).intValue());
                landMass.setAmount(treeWellDao.getCountlandmass(landMass.getLandMass_id()).intValue());
            }
        }
        return Msg.newMsg(all);
    }

    @GetMapping("/listlandbyarea")
    public Msg<ApiLandList> listlandbyarea(@RequestParam long areaid) {
        ApiLandList res = new ApiLandList();
        res.setEmptylist(landMassService.listisEmptyLandMassandarea(0, areaid));
        List<ApiLandMassModel> noemptylist = landMassService.listisEmptyLandMassandarea(1, areaid);
        for (ApiLandMassModel apiLandMassModel : noemptylist) {
            int treeWell_occupy = apiLandMassModel.getTreeWell_occupy();
            int treeWell_quantity = apiLandMassModel.getTreeWell_quantity();
            if (treeWell_occupy == 0) {
                res.getTreeareaemptylist().add(apiLandMassModel);
            } else if (treeWell_occupy < treeWell_quantity) {
                res.getNofulllist().add(apiLandMassModel);
            } else {
                res.getFulllist().add(apiLandMassModel);
            }
        }
        return Msg.newMsg(res);
    }

    @ApiOperation(value = "查询区域id获取地块", notes = "查询区域id获取地块")
    @GetMapping("/listlandmass")
    public Msg<List<LandMass>> listlandmass(@RequestParam long areaid) {
        List<LandMass> landMasses = new ArrayList<>();
        AreaLocation areaLocation = areaLocationDao.findOne(areaid);
        if (Objects.nonNull(areaLocation)) {
            landMasses.addAll(areaLocation.getLandMasses());
        }
        return Msg.newMsg(landMasses);
    }
    public static final int NORTH=1;
    public static final int SOUTH=2;
    public static final int EAST=3;
    public static final int WEST=4;
    @GetMapping("/generateland")
    public List<Drawland> generateland(@RequestParam Long areaname,@RequestParam int w,@RequestParam int h,@RequestParam int roadw,@RequestParam boolean isbei,@RequestParam boolean isnan,@RequestParam boolean isxi,@RequestParam boolean isdong){
        List<Drawland> landlist=new ArrayList<>();
        AreaLocation area= areaLocationDao.findOne(areaname);
        double length = area.getLength();
        double breadth = area.getBreadth();
        double maxlength=length;
        double maxbreath=breadth;
        int startx=0,starty=0,basex=0,basey=0,rowh=h;
        if(isbei){
            starty+=roadw;
            basey+=roadw;
        }if(isxi){
            startx+=roadw;
            basex+=roadw;
        }if(isnan){
              maxlength=length-roadw;
        }if(isdong){
              maxbreath=breadth-roadw;
        }
        int landindex=0;
        while (starty<maxlength){
            Drawland drawland=new Drawland();
            if(startx<maxbreath){
                if(startx!=basex){
                    Drawland lastland = landlist.get(landindex-1);
                    if(lastland.getRightmargin()==0&&lastland.getLeftmargin()==0){
                        lastland.setRightmargin(roadw);
                        startx+=roadw;
                        drawland.setLeftmargin(roadw);
                    }
                }
                if(startx+w<=maxbreath) {
                    drawland.setName(landindex+1);
                    drawland.setXpos(startx);
                    drawland.setYpos(starty);
                    drawland.setW(w);
                    drawland.setH(h);
                    landlist.add(drawland);
                    landindex++;
                    startx+=w;
                }else if(startx+3<maxbreath){
                    drawland.setName(landindex+1);
                    drawland.setXpos(startx);
                    drawland.setYpos(starty);
                    drawland.setW((int) (maxbreath-startx));
                    drawland.setH(h);
                    landlist.add(drawland);
                    landindex++;
                    startx+=maxbreath-startx;
                }
            }else{
                starty+=h;
                if(starty+h<=maxlength) {
                    startx=basex;
                    drawland.setName(landindex+1);
                    drawland.setXpos(startx);
                    drawland.setYpos(starty);
                    drawland.setW(w);
                    drawland.setH(h);
                    drawland.setLeftmargin(0);
                    landlist.add(drawland);
                    startx+=w;
                    landindex++;
                }else if(starty+3<maxlength){
                    startx=basex;
                    h= (int) (maxlength-starty);
                    drawland.setName(landindex+1);
                    drawland.setXpos(startx);
                    drawland.setYpos(starty);
                    drawland.setW(w);
                    drawland.setLeftmargin(0);
                    drawland.setH(h);
                    landlist.add(drawland);
                    landindex++;
                    startx+=w;
                }
            }
        }
        return landlist;
    }
    @PostMapping("/upload")
    public String upload(@RequestBody WebLandlist webland){
        AreaLocation areaLocation = areaLocationDao.findOne(webland.getAreaid());
        for(Drawland drawland:webland.getLands()){
            LandMass landMass = landMassDao.findSerialNumber(webland.getAreaid(), drawland.getName() + "");
            if(landMass==null){
                landMass=new LandMass();
            }
            landMass.setXcoordinate(drawland.getXpos());
            landMass.setYcoordinate(drawland.getYpos());
            landMass.setBreadth(drawland.getW());
            landMass.setLength(drawland.getH());
            landMass.setAreaLocation(areaLocation);
            landMass.setLeftmargin(drawland.getLeftmargin());
            landMass.setRightmargin(drawland.getRightmargin());
            landMass.setSerialNumber(drawland.getName()+"");
            landMassDao.saveAndFlush(landMass);
        }
        return "success";
    }
    @GetMapping("/addroad")
    public String addroad(@RequestParam long areaid,@RequestParam String landnumber,@RequestParam int roadwidth){
        if(TextUtils.isEmpty(landnumber)) return "fail";
        String[] split = landnumber.split("-");
        int ser = Integer.parseInt(split[0]);
        int max=Integer.parseInt(split[1]);
        for(int i=ser;i<=max;i++){
            LandMass landmass = landMassDao.findSerialNumber(areaid, i+"");
            if(landmass!=null) {
                double length = landmass.getLength();
                if (length > roadwidth) {
                    landmass.setLength(length - roadwidth);
                }
                landMassDao.saveAndFlush(landmass);
            }
        }
         return "success";
    }
}
